package com.android.download.adapter

import com.chad.library.adapter.base.BaseQuickAdapter

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.view.View
import android.widget.ImageView
import android.widget.ProgressBar
import android.widget.TextView
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer
import com.android.download.R
import com.android.download.entity.AppInfo
import com.android.lib.download.DownloadManager
import com.android.lib.download.entity.DownloadInfo
import com.bumptech.glide.Glide

class MainAdapter(
    layoutResId: Int = R.layout.item_app_home,
    data: MutableList<AppInfo>?,
    val lifecycleOwner: LifecycleOwner
) : BaseQuickAdapter<AppInfo, DownloadViewHolder>(layoutResId, data) {


    override fun convert(helper: DownloadViewHolder, item: AppInfo) {
        helper.tag = item.downloadUrl!!

        val ivIcon = helper.getView<ImageView>(R.id.iv_icon)
        Glide.with(context)
            .load(item.icon)
            .placeholder(ColorDrawable(Color.BLACK))
            .into(ivIcon)

        helper.setText(R.id.tv_name, item.name)

        val downloadTask = DownloadManager.getDownloadTask(url = helper.tag)
        downloadTask?.observer(lifecycleOwner, DownloadObserver(helper.tag, helper))
        val tbStatus = helper.getView<TextView>(R.id.tb_status)
        tbStatus.text = "下载"
        tbStatus.setTextColor(Color.argb(0xFF, 0x11, 0xB0, 0x77))
        val downloadInfo = downloadTask?.getDownloadInfo()
        val progressBar = helper.getView<ProgressBar>(R.id.progress_bar)
        progressBar.visibility = if (downloadInfo == null) View.GONE else View.VISIBLE

        downloadInfo?.let {
            progressBar.progress = it.progress
            when (it.status) {
                DownloadInfo.DOWNLOADING -> {
                    tbStatus.text = String.format("%s%s", it.progress, "%")
                    progressBar.visibility = View.VISIBLE
                    tbStatus.setTextColor(Color.argb(0xFF, 0x11, 0xB0, 0x77))
                    tbStatus.setBackgroundResource(R.drawable.bt_background_normal)
                }
                DownloadInfo.PAUSE -> {
                    tbStatus.text = "继续"
                    progressBar.visibility = View.VISIBLE
                    tbStatus.setTextColor(Color.argb(0xFF, 0x11, 0xB0, 0x77))
                    tbStatus.setBackgroundResource(R.drawable.bt_background_normal)
                }
                DownloadInfo.WAITING -> {
                    tbStatus.text = "等待"
                    progressBar.visibility = View.VISIBLE
                    tbStatus.setTextColor(Color.argb(0xFF, 0x11, 0xB0, 0x77))
                    tbStatus.setBackgroundResource(R.drawable.bt_background_normal)
                }
                DownloadInfo.ERROR -> {
                    tbStatus.text = "重试"
                    progressBar.visibility = View.GONE
                    tbStatus.setTextColor(Color.RED)
                    tbStatus.setBackgroundResource(R.drawable.bt_background_error)
                }
                DownloadInfo.DONE -> {
                    tbStatus.text = "完成"
                    tbStatus.setTextColor(Color.DKGRAY)
                    progressBar.visibility = View.GONE
                    tbStatus.setBackgroundResource(R.drawable.bt_background_done)
                }
                else -> {
                    tbStatus.text = "下载"
                    progressBar.visibility = View.GONE
                    tbStatus.setTextColor(Color.argb(0xFF, 0x11, 0xB0, 0x77))
                    tbStatus.setBackgroundResource(R.drawable.bt_background_normal)
                }
            }
        }
    }

    inner class DownloadObserver(private val tag: Any, private val holder: DownloadViewHolder) :
        Observer<DownloadInfo> {

        override fun onChanged(t: DownloadInfo?) {
            if (tag == holder.tag) {
                try {
                    notifyItemChanged(holder.adapterPosition)
                } catch (e: Exception) {
                }
            }
        }
    }
}